package com.jrmf.redenvelopes.order.util;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class ExcelUtil {
    public static final int PER_SHEET_LIMIT = 500000;
    public static SXSSFWorkbook getSXSSFWorkbookByPageThread(String[] title, String[][] values, List<Map<String,Object>> accountList) {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        int pageNum = accountList.size() / PER_SHEET_LIMIT;
        int lastCount = accountList.size() % PER_SHEET_LIMIT;
        if (accountList.size() > PER_SHEET_LIMIT) {
            CellStyle style = wb.createCellStyle();
            int sheet = lastCount == 0 ? pageNum : pageNum + 1;
            CountDownLatch downLatch = new CountDownLatch(sheet);
            Executor executor = Executors.newFixedThreadPool(sheet);
            for (int c = 0; c <= pageNum; c++) {
                int rowNum = PER_SHEET_LIMIT;
                if (c == pageNum) {
                    if (lastCount == 0) {
                        continue;
                    }
                    rowNum = lastCount;
                }
                Sheet shee = wb.createSheet("page" + c);
                executor.execute(new PageTask(downLatch, shee, title, style, rowNum,values, accountList));
            }
            try {
                downLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        else{
            CellStyle style = wb.createCellStyle();
            Sheet shee = wb.createSheet("page" + 0);
            Row row = shee.createRow(0);
            Cell cell = null;
            for (int i = 0; i < title.length; i++) {
                cell = row.createCell(i);
                cell.setCellValue(title[i]);
                cell.setCellStyle(style);
            }
            for (int i = 0; i < accountList.size(); i++) {
                row = shee.createRow(i + 1);
                for (int j = 0; j < 7; j++) {//todo
                    if (j==0){
                        row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("accountType")));
                    }
                    else if (j==1){
                        row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("tradeTime")));
                    }
                    else if (j==2){
                        row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("tradeId")));
                    }
                    else if (j==3){
                        if ("2".equals(accountList.get(i).get("accountType").toString())||"3".equals(accountList.get(i).get("accountType").toString())){
                            row.createCell(j).setCellValue(Double.valueOf(accountList.get(i).get("price").toString())/100);
                        }
                        else {
                            row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("price")));
                        }
                    }
                    else if (j==4){
                        if (null!=accountList.get(i).get("payReturnStatus") &&!"".equals(accountList.get(i).get("payReturnStatus").toString()) ){
                            row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("payReturnStatus")));
                        }
                    }
                    else if (j==5){
                        if (null!=accountList.get(i).get("payReturnCode") &&!"".equals(accountList.get(i).get("payReturnCode").toString()) ){
                            row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("payReturnCode")));
                        }
                    }
                    else if (j==6){
                        if (null!=accountList.get(i).get("payReturnDesc") &&!"".equals(accountList.get(i).get("payReturnDesc").toString()) ){
                            row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("payReturnDesc")));
                        }
                    }
                    //row.createCell(j).setCellValue(values[i][j]);
                }
            }
        }
        return wb;
    }
}
